<?php

class ResidentNews extends DataObject implements PermissionProvider
{
    private static $singular_name = "News";

    private static $plural_name = "News";

    private static $db = array(
        "NewsTitle" => "Varchar(1000)",
        "NewsContent" => "HTMLText(4000)",
        "NewsStatus" => "Enum(array('active', 'deactivated'))",
        "StartOn" => "SS_Datetime",
        "Expires" => "SS_Datetime",
    );

    private static $searchable_fields = array(
        "NewsTitle",
        "NewsStatus",
        "Created",
        "StartOn",
        "Expires"
    );

    private static $summary_fields = array(
        "NewsTitle",
        "FormattedNewsStatus",
        "Created.Nice24",
        "StartOn.Nice24",
        "Expires.Nice24"
    );

    private static $default_sort = "ID DESC";

    private static $field_labels = array(
        "NewsTitle" => "Title",
        "FormattedNewsStatus" => "Status",
        "Created.Nice24" => "Posted Date",
        "StartOn.Nice24" => "Start Date",
        "Expires.Nice24" => "Expiry Date"
    );

    public function canView($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "VIEW_ResidentNews"
        ), "any", $member);
    }

    public function canEdit($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "EDIT_ResidentNews"
        ), "any", $member);
    }

    public function canDelete($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "DELETE_ResidentNews"
        ), "any", $member);
    }

    public function canCreate($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "CREATE_ResidentNews"
        ), "any", $member);
    }

    public function getFormattedNewsStatus($status = null)
    {
        if ($status == null) {
            $status = $this->NewsStatus;
        }

        switch ($status) {
            case Globals::STATUS_ACTIVE:
                return "Published";
            case Globals::STATUS_DEACTIVATED:
                return "Closed";
            default:
                return "-";
        }
    }

    public function getDefaultSearchContext()
    {
        $context = parent::GetDefaultSearchContext();

        $context->removeFieldByName("NewsStatus");
        $context->addField($this->getNewsStatusField(true));

        return $context;
    }

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->renameField("NewsContent", "Content");

        $startOnField = new DatetimeField("StartOn", "Start Date");
        $startOnField->setConfig("datavalueformat", "MMM d, y HH:mm");
        $startOnField->getDateField()->setAttribute("placeholder", "MMM d, y");
        $startOnField->getDateField()->setConfig("showcalendar", true);
        $startOnField->getDateField()->setDescription("e.g. " . date("M d, Y"));
        $startOnField->getTimeField()->setAttribute("placeholder", "HH:mm");
        $startOnField->getTimeField()->setDescription("e.g. 23:44");

        $fields->replaceField("StartOn", $startOnField);

        $expiryField = new DatetimeField("Expires", "Expiry Date");
        $expiryField->setConfig("datavalueformat", "MMM d, y HH:mm");
        $expiryField->getDateField()->setAttribute("placeholder", "MMM d, y");
        $expiryField->getDateField()->setConfig("showcalendar", true);
        $expiryField->getDateField()->setDescription("e.g. " . date("M d, Y"));
        $expiryField->getTimeField()->setAttribute("placeholder", "HH:mm");
        $expiryField->getTimeField()->setDescription("e.g. 23:44");

        $fields->replaceField("Expires", $expiryField);

        $statusField = HiddenField::create();
        $statusField->setName("NewsStatus");
        $statusField->setValue("active");

        $fields->addFieldToTab("Root.Main", $statusField);

        if ($this->isInDB()) {
            $fields->replaceField("NewsStatus", $this->getNewsStatusField());
        } else {
            $fields->removeByName("NewsStatus");
        }

        return $fields;
    }

    public function getNewsStatusField($showAny = false)
    {
        $arr = array(
            "" => "(Any)",
            "active" => "Published",
            "deactivated" => "Closed"
        );

        if (!$showAny) {
            unset($arr[""]);
        }

        $field = new DropdownField
        (
            "NewsStatus",
            _t("General.NEWS_STATUS", "Status"),
            $arr
        );

        return $field;
    }

    public function providePermissions()
    {
        $category = "Manage resident news permissions";

        return array(
            "VIEW_ResidentNews" => array(
                "name" => "View all records",
                "category" => $category
            ),
            "CREATE_ResidentNews" => array(
                "name" => "Create new record",
                "category" => $category
            ),
            "EDIT_ResidentNews" => array(
                "name" => "Edit record",
                "category" => $category
            ),
            "DELETE_ResidentNews" => array(
                "name" => "Delete record",
                "category" => $category
            )
        );
    }
}